import axios from 'axios'

import { Message } from "element-ui";
import 'element-ui/lib/theme-chalk/index.css'
import {getToken} from "./token";
import store from "../store";

const request = axios.create({
	baseURL:'http://g2s.hfjava.cn:8080/api/',
})

request.interceptors.request.use(config=>{
  if (getToken()){
    config.headers.token = getToken()
  }
  return config
})

//响应拦截器
request.interceptors.response.use(async (response)=>{
	// console.log(response)
	const code = response.data.code
	if(code !== "0"){
	  if (code === "1"){
      Message.error(response.data.msg)
    }
		if(code === "500"){
			Message.error(response.data.msg)
		}
		if(code === "401"){//返回错误
		  // 登录过期
			Message.warning(response.data.msg)
      await store.dispatch('logout')
		}
    return Promise.reject('请求失败')
  }else{
		return response
	}
},(err)=>{
  Message.error(err)
})
export default request
